using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodeFactory.Library;
using CodeFactory.Library.Config;
using CodeFactory.Library.Language;

namespace CodeFactory.UI
{
    public partial class SystemSettingForm : Form
    {
        public SystemSettingForm()
        {
            InitializeComponent();
            this.InitCommonText();
            this.InitConfigData();
        }

        private void InitCommonText()
        {
            try
            {
                this.Text = LanguageManager.StringManager["SystemSettingForm_Text"];
                this.group_system.Text = LanguageManager.StringManager["SystemSettingForm_Group"];
                this.lable_path.Text = LanguageManager.StringManager["SystemSettingForm_Label_Path"];
                this.label_table.Text = LanguageManager.StringManager["SystemSettingForm_Label_Table"];
                this.lable_attribute.Text = LanguageManager.StringManager["SystemSettingForm_Label_Attribute"];
                this.lable_class.Text = LanguageManager.StringManager["SystemSettingForm_Label_Class"];
                this.label_defaultvalue.Text = LanguageManager.StringManager["SystemSettingForm_Label_DefaultValue"];
                this.lable_assemblie.Text = LanguageManager.StringManager["SystemSettingForm_Lable_Assemblie"];
                this.button_add.Text = LanguageManager.StringManager["SystemSettingForm_Button_Add"];
                this.button_save.Text = LanguageManager.StringManager["SystemSettingForm_Button_Save"];
                this.button_cancel.Text = LanguageManager.StringManager["SystemSettingForm_Button_Cacel"];
            }
            catch (Exception ex)
            {
                this.ShowErr("SystemSettingForm Init Error.", ex.Message);
                Environment.Exit(-1);
            }
        }

        private void InitConfigData()
        {
            try
            {
                this.textbox_path.Text = ConfigManager.Info.SysEncoder.CodeFile;
                this.textbox_table.Text = ConfigManager.Info.SysEncoder.TableMethod;
                this.textbox_attribute.Text = ConfigManager.Info.SysEncoder.AttributeMethod;
                this.textbox_class.Text = ConfigManager.Info.SysEncoder.ClassMethod;
                this.textbox_defaultvalue.Text = ConfigManager.Info.SysEncoder.DefaultValueMethod;
                this.textbox_assemblie.Text = string.Empty;
                this.list_assemblie.Items.Clear();
                List<string> assList = ConfigManager.Info.SysEncoder.AssemblieList;
                foreach (string s in assList)
                {
                    this.list_assemblie.Items.Add(s);
                }
                if (this.list_assemblie.Items.Count > 0)
                {
                    this.deleteallToolStripMenuItem.Enabled = true;
                }
                else
                {
                    this.deleteallToolStripMenuItem.Enabled = false;
                }
                if (this.list_assemblie.SelectedItem != null)
                {
                    this.deleteToolStripMenuItem.Enabled = true;
                }
                else
                {
                    this.deleteToolStripMenuItem.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.ShowErr("SystemSettingForm Init Error.", ex.Message);
                Environment.Exit(-1);
            }
        }

        private void ShowErr(string title, string message)
        {
            MessageBox.Show(this, message, title, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void list_assemblie_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.list_assemblie.SelectedItem != null)
            {
                this.deleteToolStripMenuItem.Enabled = true;
            }
            else
            {
                this.deleteToolStripMenuItem.Enabled = false;
            }
        }

        private void deleteallToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.list_assemblie.Items.Clear();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.list_assemblie.SelectedItem != null && !this.list_assemblie.SelectedItem.Equals("System.dll"))
                this.list_assemblie.Items.Remove(this.list_assemblie.SelectedItem);
        }

        private void button_add_Click(object sender, EventArgs e)
        {
            if (this.textbox_assemblie.Text.Length < 1)
            {
                this.ShowErr(LanguageManager.StringManager["Error_System_Title"], LanguageManager.StringManager["SystemForm_Assemblie_Null"]);
                return;
            }
            this.list_assemblie.Items.Add(this.textbox_assemblie.Text);
            this.textbox_assemblie.Text = string.Empty;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (this.textbox_attribute.Text.Length < 1 || this.textbox_class.Text.Length < 1
                || this.textbox_defaultvalue.Text.Length < 1 || this.textbox_path.Text.Length < 1
                || this.textbox_table.Text.Length < 1 || this.list_assemblie.Items.Count<1)
            {
                this.ShowErr(LanguageManager.StringManager["Error_System_Title"], LanguageManager.StringManager["SystemSettingForm_Msg_Null"]);
                return;
            }
            ConfigManager.Info.SysEncoder.AttributeMethod = this.textbox_attribute.Text;
            ConfigManager.Info.SysEncoder.ClassMethod = this.textbox_class.Text;
            ConfigManager.Info.SysEncoder.CodeFile = this.textbox_path.Text;
            ConfigManager.Info.SysEncoder.DefaultValueMethod = this.textbox_defaultvalue.Text;
            ConfigManager.Info.SysEncoder.TableMethod = this.textbox_table.Text;
            bool isSystemDll = false;
            ConfigManager.Info.SysEncoder.AssemblieList.Clear();
            foreach (object o in this.list_assemblie.Items)
            {
                if (o.Equals("System.dll"))
                {
                    isSystemDll = true;
                }
                ConfigManager.Info.SysEncoder.AssemblieList.Add(o.ToString());
            }
            if (!isSystemDll)
            {
                ConfigManager.Info.SysEncoder.AssemblieList.Add("System.dll");
                this.list_assemblie.Items.Add("System.dll");
            }
            ConfigManager.SaveConfig(ConfigManager.Info);
            this.Hide();
        }
    }
}